using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
//using Ntx.GD;

namespace TileSetConverter
{
    /// <summary>
    /// Parses Dune 2000 R8 files
    /// </summary>
    /// 
    public class TileSetR8 : TileSet
    {
        #region Variables

        Palette palette;

        internal Palette Palette
        {
            get { return palette; }
            set { palette = value; }
        }
        #endregion

        /// <summary>
        /// Begins parsing  an R16 file
        /// </summary>
        /// <param name="filepath">Location of the file</param>
        /// <param name="device">Graphics device (used for creating textures)</param>
        public TileSetR8(string filepath, string paletteFilename)
        {
            this.filepath = filepath;

            palette = new Palette(paletteFilename);

            br = new BinaryReader(File.Open(filepath, FileMode.OpenOrCreate));
            tiles = new List<TileSetTile>();
            for (int i = 0; i < 800; i++)
            {
                tiles.Add(ReadBlock());
                //textures.Add(ReadBlock());
            }
            br.Close();
        }

        /// <summary>
        /// Reads one block of information about a tile.
        /// </summary>
        /// <returns>A texture for that block. Later change this to a Tile class.</returns>
        private TileSetTile ReadBlock()
        {
            //If the block doesn't start with 0x01, then we've got issues :)
            if (br.ReadByte() != 1)
            {
                throw new Exception("Tried to read new block in " + filepath + " failed. Assertion error - block didn't start with 0x01.");
            }

            Int32 width, height;
            width = br.ReadInt32();
            height = br.ReadInt32();

            Bitmap bitmap = new Bitmap(width, height);
            List<byte> retBytes = new List<byte>();
            int size = width * height;
            retBytes.Add(1);
            retBytes.AddRange(BitConverter.GetBytes(width));
            retBytes.AddRange(BitConverter.GetBytes(height));
            //Read in the 20 byte header of the block
            for (int i = 0; i < 20; i++)
            {
                //Discard it as we have no fucking clue what it means. We can implement
                //this later
                retBytes.Add(br.ReadByte());
            }

            byte colorIndex;
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    colorIndex = br.ReadByte();

                    bitmap.SetPixel(x, y, palette.ColorPalette[colorIndex]);
                }
            }

            return new TileSetTile(bitmap, retBytes.ToArray());
        }
        protected override void SaveBlock(BinaryWriter bw, int index)
        {
            bw.Write((byte)1);
            bw.Write(tiles[index].width);
            bw.Write(tiles[index].height);

            bw.Write(tiles[index].xOffset);
            bw.Write(tiles[index].yOffset);
            bw.Write(tiles[index].imageHandle);
            bw.Write(tiles[index].paletteHandle);
            bw.Write(tiles[index].bitsPerPixel);
            bw.Write(tiles[index].frameWidth);
            bw.Write(tiles[index].frameHeight);
            bw.Write(tiles[index].alignment);


            for (int y = 0; y < tiles[index].height; y++)
            {
                for (int x = 0; x < tiles[index].width; x++)
                {
                    byte paletteIndex = 0;

                    Color color = tiles[index].bitmap.GetPixel(x, y);
                    for (byte i = 0; i < 256; i++)
                    {
                        if (palette.ColorPalette[i] == color)
                        {
                            paletteIndex = i;
                            break;
                        }
                    }
                    bw.Write(paletteIndex);
                }
            }
        }        
    }
}